﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;

namespace Runda.EShop.SaaS.CustomerManage
{
    public partial class CustomerSetting :BasePage
    {
        public Guid CustomerID
        {
            get {
                if (Request.QueryString["CustomerID"] != null)
                {
                    return new Guid(Request.QueryString["CustomerID"]);
                }
                return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            tvProductCate.Attributes.Add("onclick", "HandleCheckEvent();");  
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.Load += new EventHandler(Page_Load);
            btnSave.Click += new EventHandler(btnSave_Click);
        }

        private void Initialize()
        {
            if (CustomerID == Guid.Empty) return;

            BizCustomerEntity entity = new CustomerBll().Get<BizCustomerEntity>(CustomerID);
            ltlCustomerName.Text = entity.CustomerName;
            ltlCustomerCode.Text = entity.CustomerCode; 

            //BindTree();
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            List<RelationCustomerProductCateEntity> list = new List<RelationCustomerProductCateEntity>();
            RelationCustomerProductCateEntity entity = null;

            foreach (TreeNode node in tvProductCate.CheckedNodes)
            {
                entity = new RelationCustomerProductCateEntity();
                entity.CustomerID = CustomerID;
                entity.Deleted = false;
                entity.CateCode = node.Value;
                entity.CateType = Int16.Parse(hidCateType.Value);

                list.Add(entity);
            }

            CustomerBll bll = new CustomerBll();
            bll.SaveCustomerCate(CustomerID,Int16.Parse(hidCateType.Value), list);

            base.DoOperationLog(Common.Utility.EnumHelper.OperationType.Edit, "维护客户产品类别，CustomerID=" + CustomerID);
            //Response.RedirectPermanent("CustomerList.aspx");
            ClientScript.RegisterStartupScript(this.GetType(), "SaveAndClose", "alert('保存成功.');parent.$.ligerDialog.hide();", true);
        }

        protected void btnCateType1_Click(object sender, EventArgs e)
        {
            int CateType= 1;
            hidCateType.Value = "1";
            BindTree(CateType);
        }

        protected void btnCateType2_Click(object sender, EventArgs e)
        {
            int CateType = 2;
            hidCateType.Value = "2";
            BindTree(CateType);
        }
        #region TreeView Method
        private void BindTree(int catetype)
        {
            tvProductCate.Nodes.Clear();
            CustomerBll bll = new CustomerBll();
            List<BasProductCateEntity> dt = bll.BuildProductCategory(CustomerID,catetype);

            List<BasProductCateEntity> rows = dt.FindAll(item => item.Grade == 2);

            foreach (BasProductCateEntity row in rows)
            {
                TreeNode node = CreateNode(row.CateName, false, row.CateCode,(row.IsChecked==1));
                tvProductCate.Nodes.Add(node);

                RecursivelyPopulate(dt, node, row);
            }
        }

        private void RecursivelyPopulate(List<BasProductCateEntity> dt, TreeNode node, BasProductCateEntity current)
        {
            List<BasProductCateEntity> rows = dt.FindAll(item => item.Grade == (current.Grade + 1) && item.CateCode.StartsWith(node.Value));
            foreach (BasProductCateEntity row in rows)
            {
                TreeNode treenode = CreateNode(row.CateName, false, row.CateCode, (row.IsChecked == 1));


                node.ChildNodes.Add(treenode);
                RecursivelyPopulate(dt, treenode, row);
            }
        }

        private TreeNode CreateNode(string text, bool expanded, string id,bool IsChecked)
        {
            TreeNode node;
            node = new TreeNode(text, id);
            node.Expanded = expanded;
            node.Checked = IsChecked;
            node.SelectAction = TreeNodeSelectAction.None;
            return node;
        }
        #endregion
    }
}